package com.demo.gateway.rest;

import java.io.IOException;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: demo
 * @date: 2020年6月23日
 * @Document : restTemplate拦截器
 */
@Slf4j
public class RequestLoggerInterceptor implements ClientHttpRequestInterceptor  {

	@Override
	public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
		StringBuilder sb=new StringBuilder();
		sb.append(String.format("\n rest  request  ,uri==>%s,method==>%s,params==>%s", request.getURI(),request.getMethod(),new String(body,"UTF-8")));
        log.debug(sb.toString());
	    ClientHttpResponse httpResponse = execution.execute(request, body);
	    log.debug(String.format("\n rest response , statusCode==>%s,statusText==>%s",httpResponse.getStatusCode().value(),httpResponse.getStatusText()));
		return httpResponse;
	}

}
